一開始想標題還有點梗
當每天做重覆的事情時
想梗反而比解題還麻煩 XD
這次Coderwars LV6
題目(Count characters in your string):
The main idea is to count all the occuring characters(UTF-8) in string. If you have string like this aba then the result should be { 'a': 2, 'b': 1 }
What if the string is empty ? Then the result should be empty object literal { }
For C#: Use a Dictionary<char, int> for this kata!
def count_chars(s)
# your code here
end
Test.assert_equals(count_chars("aba"), {"a" => 2, "b" => 1})
Test.assert_equals(count_chars(""), {})
答案:
# Count characters in your string
def count_chars(s)
s.split('').map{ |i| ["#{i}", s.count(i)] }.to_h
end
本文同步發布於 小菜的 Blog https://riverye.com/